<?php
class Company_thumbnail_model extends MY_Model {

    function __construct()
    {
        parent::__construct();
        $this->make_dirs();
    }
    
	function get_max_id($data = null)
    {
    	$this->db->select_max('id');
    	
    	if (isset($data['idmember']) && is_numeric($data['idmember'])) {
    		$this->db->where('company_thumbnail.idmember', $data['idmember']);
    	}
    	
    	return $this->db->get('company_thumbnail')->row()->id;
    }
    
    function get($data)
    {
    	if (isset($data['idimage']) && is_numeric($data['idimage'])) {
    		$this->db->where('company_thumbnail.id', $data['idimage']);
    	}
    	
    	if (isset($data['idmember']) && is_numeric($data['idmember'])) {
    		$this->db->where('company_thumbnail.idmember', $data['idmember']);
    	}
    	
    	$this->db->order_by('company_thumbnail.idnow', 'desc');
    	$this->db->order_by('company_thumbnail.id', 'desc');
    	
    	return $this->db->get('company_thumbnail');
    }
    
	function reset_thumbnail($data)
    {
    	$this->db->set('idnow', 'N');
		$this->db->where('idmember', $data['idmember']);
		$this->db->update('company_thumbnail');
    }
    
	function update_thumbnail($data)
    {
    	$this->db->trans_begin();
    	
    	$this->reset_thumbnail($data);
    	
    	$this->db->set('idnow', 'Y');
		$this->db->where('id', $data['id']);
		
		$this->db->update('company_thumbnail');
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    $this->load_session_thumbnail();
		    return true;
		}
    }
    
	function insert($data)
    {
    	$this->db->trans_begin();
    	
    	$extension = end(explode('.', $data['image_name']));
		
		$image_path = FCPATH . 'f/' . $data['idmember'] . '/tmb';
		
		if (!is_dir($image_path))
		{
			mkdir($image_path);
		}
		
		if (isset($data['tmp_file']))
		{
			$tmp_path = $data['tmp_path'];
			$tmp_file = $data['tmp_file'];
		}
		else
		{
			$tmp_path = FCPATH . 'tmp/' . $data['key'];
			$tmp_file = $tmp_path . '/' . $data['image_name'];
			
			if (!is_dir($tmp_path))
			{
				mkdir($tmp_path);
			}
			
			move_uploaded_file($data['image'], $tmp_file);
		}
		
		$image_prefix = $this->get_max_id() . $tmp_file;
		
		$thumbnail = md5($image_prefix . '_thumb').'.'.$extension;
    	$config = array();
    	$config['image_library'] = 'gd2';
		$config['source_image'] = $tmp_file;
		$config['new_image'] = $image_path . '/' . $thumbnail;
		$config['maintain_ratio'] = TRUE;
		$config['width'] = 50;
		$config['height'] = 50;
		
		$this->load->library('image_lib', $config);
		$this->image_lib->resize();
		$this->image_lib->clear();
		
		$image = md5($image_prefix . '_image').'.'.$extension;
		$config = array();
    	$config['image_library'] = 'gd2';
		$config['source_image'] = $tmp_file;
		$config['new_image'] = $image_path . '/' . $image;
		$config['maintain_ratio'] = TRUE;
    	$config['width'] = 180;
		$config['height'] = 180;
		
		$this->image_lib->initialize($config);
		$this->image_lib->resize();
		
		unlink($tmp_file);
		rmdir($tmp_path);
		
		$this->reset_thumbnail($data);
		
		$data_insert = array(
		   'thumbnail' => $thumbnail,
		   'image' => $image,
		   'idmember' => $data['idmember'],
    	   'idnow' => 'Y'
		);
		
		$this->db->set('date', 'NOW()', false);
		$this->db->insert('company_thumbnail', $data_insert);
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    $this->load_session_thumbnail();
		    return true;
		}
    }
    
	function delete($data)
    {
    	$this->db->trans_begin();
    	
    	$thumbnail_data = $this->get(array('idimage' => $data['id']))->row();
    	
    	$data_delete = array(
		   'id' => $data['id']
		);
		
		$this->db->delete('company_thumbnail', $data_delete);
		
		if ($thumbnail_data->idnow == 'Y')
		{
			$data_update = array();
			$data_update['idmember'] = $this->session->userdata['idmember'];
			$data_update['id'] = $this->get_max_id(array('idmember' => $data_update['idmember']));
			if (is_numeric($data_update['id']))
			{
				$this->update_thumbnail($data_update);
			}
		}
		
    	if ($this->db->trans_status() === false)
		{
		    $this->db->trans_rollback();
		    return false;
		}
		else
		{
		    $this->db->trans_commit();
		    $thumbnail_path = FCPATH . 'f/' . $this->session->userdata['idmember'] . '/tmb';
	    	unlink($thumbnail_path . '/' . $thumbnail_data->thumbnail);
	    	unlink($thumbnail_path . '/' . $thumbnail_data->image);
	    	$this->load_session_thumbnail();
		    return true;
		}
    }
    
    function load_session_thumbnail()
    {
    	$thumbnail_data = $this->get(array('idmember' => $this->session->userdata['idmember']))->row();
    	
    	if ($thumbnail_data)
    	{
    		$this->session->set_userdata(array('thumbnail' => $thumbnail_data->thumbnail));
    	}
    	else
    	{
    		$this->session->unset_userdata('thumbnail');
    	}
    }
}
?>